package common.com.androidbase.download;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.common.sdk.net.download.interfaces.IDownloadBuilder;
import com.common.sdk.net.download.request.model.DownloadInfo;

import common.com.androidbase.R;

public class MyAdapter extends BaseAdapter {

    private List<DownloadInfo> list;
    private LayoutInflater mInflater;
    private Context mContext;
    private IDownloadBuilder downloadRequest;

    public MyAdapter(Context mContext, List<DownloadInfo> list, IDownloadBuilder downloadRequest) {
        this.mContext = mContext;
        this.list = list;
        this.downloadRequest = downloadRequest;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public DownloadInfo getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_adapter_download, parent, false);
            holder.initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DownloadInfo downloadInfo = getItem(position);
        holder.downloadInfo = downloadInfo;
        holder.list_adapter_textview.setText("优先级：" + downloadInfo.getDownloadPriority() + " 时间："
                + downloadInfo.getRequestStartTime() + " 地址：" + downloadInfo.getDownloadPath());
        holder.list_text_pro.setText(downloadInfo.errorCode);
        holder.progressBar1.setProgress(0);
        if (downloadInfo != null && downloadInfo.getFileSize() != 0) {
            holder.progressBar1.setProgress(downloadInfo.getDownloadPercentage());
        }
        return convertView;
    }

    class ViewHolder implements OnClickListener {

        public DownloadInfo downloadInfo;
        private TextView list_adapter_textview;
        private ProgressBar progressBar1;
        private Button list_button_pausedownloaditem;
        private Button list_button_startdownloaditem;
        private TextView list_text_pro;
        private Button list_button_delete;

        private void initView(View view) {
            list_adapter_textview = (TextView) view.findViewById(R.id.list_adapter_textview);
            progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);
            list_button_pausedownloaditem = (Button) view.findViewById(R.id.list_button_pausedownloaditem);
            list_button_startdownloaditem = (Button) view.findViewById(R.id.list_button_startdownloaditem);
            list_button_delete = (Button) view.findViewById(R.id.list_button_delete);
            list_text_pro = (TextView) view.findViewById(R.id.list_text_pro);

            list_button_pausedownloaditem.setOnClickListener(this);
            list_button_startdownloaditem.setOnClickListener(this);
            list_button_delete.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int cancelUum2;
            switch (v.getId()) {
                case R.id.list_button_pausedownloaditem:
                    /*int  cancelUum = */downloadRequest.pauseDownloadItem(downloadInfo);
                    // if (cancelUum == DownloadState.OPERATION_SUCCESS) {
                    // // 操作成功
                    // } else if (cancelUum ==
                    // DownloadState.OPERATION_ALREADY_EXIST) {
                    // // 已经取消过
                    // } else if (cancelUum == DownloadState.OPERATION_FAILED) {
                    // // 操作失败
                    // }
                    break;
                case R.id.list_button_startdownloaditem:
                    /*cancelUum2 = */downloadRequest.startDownloadItem(downloadInfo);
                    // if (cancelUum2 == DownloadState.OPERATION_SUCCESS) {
                    // // 操作成功
                    // } else if (cancelUum2 ==
                    // DownloadState.OPERATION_ALREADY_EXIST) {
                    // // 已经取消过
                    // } else if (cancelUum2 == DownloadState.OPERATION_FAILED)
                    // {
                    // // 操作失败
                    // } else if (cancelUum2 ==
                    // DownloadState.OPERATION_SUCCESSED) {
                    // // 已经下载成功
                    // }
                    break;
                case R.id.list_button_delete:
                    downloadRequest.deleteDownloadItem(downloadInfo);
                    break;
            }
        }
    }
}
